package run;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import vcp.application.ApplicationLoader;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.awt.Color;
import java.awt.Font;

/**
 * Client frame initializes client connection to server
 *
 */
public class ClientFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textAddr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientFrame frame = new ClientFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientFrame() {
		super("VCP client at :"+InetAddress.getLoopbackAddress().getHostAddress());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 150);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLUE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblServerHost = new JLabel("Server address:");
		lblServerHost.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblServerHost.setForeground(Color.WHITE);
		lblServerHost.setToolTipText("Please check server address at server GUI");
		lblServerHost.setBounds(12, 13, 151, 16);
		contentPane.add(lblServerHost);
		
		textAddr = new JTextField();
		textAddr.setToolTipText("ec \"localhost\" or \"127.0.0.1\" or \"168.192.0.1\" ...");
		textAddr.setBounds(133, 13, 287, 22);
		contentPane.add(textAddr);
		textAddr.setColumns(10);
		
		JButton btnConnect = new JButton("connect");
		btnConnect.setForeground(Color.BLACK);
		btnConnect.setBackground(Color.WHITE);
		btnConnect.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnConnect.setToolTipText("Please check server address at server gui");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				EventQueue.invokeLater(new Runnable() {
					@SuppressWarnings("unused")
					public void run() {
						try {
							String host = textAddr.getText();
							if(host.isEmpty()){
								JOptionPane.showMessageDialog(null,
										"Please specify server address...", "Error",
										JOptionPane.ERROR_MESSAGE); 
							}else{
							  ApplicationLoader loader = new ApplicationLoader(host);
							  setVisible(false);
							  dispose();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		btnConnect.setBounds(323, 42, 97, 25);
		contentPane.add(btnConnect);
	}
}
